$(function(){ 

    //var log = console.log; 
    var $content = $('#content');  

    //The Form itself
    $content.find('#frm_wordofday').wl_Form({
        submitButton: 'button.submit',
        resetButton: 'button.reset',
        method: 'post',
        action: null,
        ajax: true,
        serialize: false,
        parseQuery: true,
        dataType: 'text',
        status: true,
        sent: false,
        confirmSend: true,
        text: {
            required: 'Ovo polje je obavezno',
            valid: 'Ovo polje nije u redu',
            password: 'Lozinka je kratka',
            passwordmatch: 'Ovaa lozinka se ne poklapa',
            fileinqueue: 'Postoji najmanje jedan fajl na čekanju',
            incomplete: 'Molimo popunite formu ispravno!',
            send: 'pošalji formu...',
            sendagain: 'pošalji ponovo?',
            success: 'forma poslata!',
            error: 'greška prilikom slanja!',
            parseerror: 'Nemogu da deserializujem query string:\n %e'
        },
        tooltip: {
            gravity: 'nw'
        },
        onRequireError: function (element) {},
        onValidError: function (element) {},
        onPasswordError: function (element) {},
        onFileError: function (element) {},
        onBeforePrepare: function () {

            $('#srvrserror-3').hide();

            var chk = $('#filename').val();
            //console.log('chk2:'+chk2)
            var chk =  true; 

            if(chk==''){ 

                $('#srvrserror-3').html('molimo odaberite fajl za upload');
                $('#srvrserror-3').show('slow');

            }else {

                // When all files are uploaded submit form
                loader();

                /***********************************************************
                * DATEPICKER 1 - POLUOBJAVA
                ***********************************************************/
                var chosen_date = $("#datumkomponenta1").datepicker("getDate");        

                //show selected date before table
                var mesec = chosen_date.getMonth()+1;                             
                datetime = chosen_date.getDate() + "-" + mesec + "-" + chosen_date.getFullYear();


                $.ajax({
                    type: "POST",
                    url: base_url+'wordofday/edit_insert',
                    data: {
                        id: get_id, 
                        author: $("#author").val(),
                        text: $("#text").val(), 
                        datetime: datetime, 
                        appmode: appmode
                    },
                    dataType: 'json',
                    success: function(data){

                        if(data.action==true){

                            $.modal.close();
                            /*----------------------------------------------------------------------*/
                            /* go to preview article url 
                            /*----------------------------------------------------------------------*/
                            document.location.href = base_url + 'wordofday/preview';

                        }else{

                            alert('Molim odaberte sva polja.'+data.msg)

                        }


                    }
                }) 

            }

            return false;
        },
        onBeforeSubmit: function (data) {},
        onReset: function () {},
        onComplete: function (textStatus, jqXHR) {},
        onError: function (textStatus, error, jqXHR) {},
        onSuccess: function (data, textStatus, jqXHR) {}
    }); 

    /***********************************************************
    * LOADERS
    ***********************************************************/    

    function loader()
    {
        $.modal(
        "<div><div class='animate'><img src='"+base_url+"assets/img/backgnds/loading.gif'></div><h2>Molim sačekajte...</h2></div>" , {
            closeHTML: "",
            containerCss:{
                height:70,
                width:120,
                borderColor:"#dedede"
            },
            opacity:20,
            overlayCss: {
                backgroundColor: "#000000"
            }
        });
    }

    function setHtml5(){

        /***********************************************************
        * Datum komponenta
        ***********************************************************/
        date = new Date();
        selMonth = 0;
        selYear = 0;
        maxMounth = 0;

        // get the current date
        var minDate = new Date(date.getFullYear(), date.getMonth(), 1); //one day next before month
        var maxDate =  new Date(date.getFullYear(), date.getMonth() +1 + maxMounth, -0); // one day before next month

        $('#datumkomponenta1').html('');
        $('#datumkomponenta1').datepicker( "destroy" );

        $.datepicker.setDefaults($.datepicker.regional['sr']);

        $('#datumkomponenta1').datepicker({
            inline: true,
            hideIfNoPrevNext: true,
            minDate: minDate,
            dateFormat: 'dd.mm.yy',
            numberOfMonths: 1,
        });

        //log('appmode:'+appmode)

        switch(appmode){

            case 'edit':
                var dt = new Date( parseInt(datetime) * 1000);
                $('#datumkomponenta1').datepicker('setDate', new Date(dt.getFullYear(), dt.getMonth(), dt.getDate()));
                break;

            case 'insert':
                $('#datumkomponenta1').datepicker('setDate', new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1));
                break;
        }        

    }

    setHtml5();

});
